// pages/riddle/riddle.js
const baseUrl = 'http://127.0.0.1:2024';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    check: 0,
    currIndex: 0,
    yes: null,
    selectList: [],
    currIndex1: 0,
    noChangeIdiomList:[],
    idiomList:[],
    inputAnswer:'',
    idiomObjArr:[],
    currIndex2: 0,
    jianList:[],
    textAnswer:'',
    jinaObjArr:[],
    cj: 0,
    objArr: [],
    answer: '',
    newArr:{},


  },

  /**
   * 生命周期函数--监听页面加载
   */
 async onLoad(options) {
  await this.getSelect()
  await this.getIdiom()
  await this.getJian()
 
  },
 async getSelect() {
    wx.request({
      url: baseUrl + '/api/getSelectQA',
      success: res => {
        console.log(res.data.message);
        this.setData({
            selectList: res.data.message
        })
        console.log(this.data.selectList);
      }
    })
  },
 async getIdiom(){
    wx.request({
        url: baseUrl + '/api/getIdiom',
        success: res => {
          console.log(res.data.message);
          this.setData({
            idiomList: res.data.message,
            noChangeIdiomList: res.data.message
          })
          console.log('12',this.data.idiomList);
          this.changeIdiom()
        }
      })
  },
  async getJian(){
    wx.request({
        url: baseUrl + '/api/getSimple',
        success: res => {
          console.log(res.data.message);
          this.setData({
         jianList: res.data.message
          })
          console.log(this.data.jianList);

        }
      })
  },
  //填空题数据处理
  changeIdiom(){
        if(this.data.idiomList.length > 0 ){
        console.log('asdasdas',this.data.idiomList);
        let list = this.data.idiomList
      
        list = list.map((item)=>{
          return  item.question.split('，')[0] 
        })
        console.log('asd',list);
        this.setData({
            idiomList:list
        })
  
        }      
  },
  //获取输入框数据
  getInput(e){
    //   console.log(e.detail.value);
    this.data.inputAnswer = e.detail.value
    //是否有值
    if(this.data.idiomObjArr.length>0){
        if(this.data.idiomObjArr[this.data.currIndex1]){
            this.data.idiomObjArr[this.data.currIndex1].inputAnswer = e.detail.value
            this.setData({
                inputAnswer:e.detail.value
            })
        }else{
            let obj = {
                inputAnswer:e.detail.value
            }
            this.data.idiomObjArr[this.data.currIndex1] = obj
        }
    }else{
        let obj = {
            inputAnswer:e.detail.value
        }
        this.data.idiomObjArr[this.data.currIndex1] = obj
    }

    console.log("111",this.data.idiomObjArr);

    
  },
  getText(e){
      console.log(e.detail.value);
    this.data.textAnswer = e.detail.value
    //是否有值
    if(this.data.jinaObjArr.length>0){
        if(this.data.jinaObjArr[this.data.currIndex2]){
            this.data.jinaObjArr[this.data.currIndex2].textAnswer = e.detail.value
            this.setData({
                textAnswer:e.detail.value
            })
        }else{
            let obj = {
                textAnswer:e.detail.value
            }
            this.data.jinaObjArr[this.data.currIndex2] = obj
        }
    }else{
        let obj = {
            textAnswer:e.detail.value
        }
        this.data.jinaObjArr[this.data.currIndex2] = obj
    }

    console.log("111",this.data.jinaObjArr);

    
  },
  check(e) {
    let id = e.currentTarget.dataset.id
    console.log(id);
    this.setData({
      current: id
    })



  },
 async choose(e) {
    let id = e.currentTarget.dataset.check
    console.log(id);
    this.setData({
      check: id
    })
    //答案
    this.data.answer = this.data.selectList[this.data.currIndex].answer
    this.data.selectList[this.data.currIndex].answer == this.data.selectList[this.data.currIndex][id] ? this.data.yes = 1 : this.data.yes = 0
    console.log(this.data.selectList[this.data.currIndex].answer);
    console.log(this.data.yes);
    //判断
      let newArr = {
      currIndex: this.data.currIndex,
      answer: this.data.answer,
      isRIght: this.data.yes,
      check: this.data.check,
    }
      
    console.log("触发前",this.data.objArr);
    console.log('当前页',this.data.currIndex);
     this.data.objArr[this.data.currIndex] = newArr
    console.table(this.data.objArr,this.data.currIndex);
 
    
  
  
   
      
    
  },
  up() {
   if(this.data.current == 1){
    if (this.data.currIndex != 0) {
        this.setData({
          currIndex: this.data.currIndex - 1
        })
  
        console.log(this.data.currIndex);
  
      } else {
        wx.showToast({
          title: '没有上一题啦!',
          icon: 'none'
        })
      }
      this.setList()
   }
   if(this.data.current == 2){
    if (this.data.currIndex1 != 0) {
        this.setData({
          currIndex1: this.data.currIndex1 - 1
        })
  
        console.log(this.data.currIndex1);
  
      } else {
        wx.showToast({
          title: '没有上一题啦!',
          icon: 'none'
        })
      }
      this.setIdiom()
    }
   if(this.data.current == 3){
    if (this.data.currIndex2 != 0) {
        this.setData({
          currIndex2: this.data.currIndex2 - 1
        })
  
        console.log(this.data.currIndex2);
  
      } else {
        wx.showToast({
          title: '没有上一题啦!',
          icon: 'none'
        })
      }
      this.setJian()
    }
   
  },
  down() {
    // console.log(this.data.current) 
    if(this.data.current == 1){
    if (this.data.currIndex < 4) {
      let currentIndex = this.data.currIndex + 1

      this.setData({

        currIndex: currentIndex
      })
      console.log("this" + this.data.currIndex);
    } else
      wx.showToast({
        title: '这是最后一道选择题~',
        icon: 'none'
      })
    this.setList()
    }
    if(this.data.current == 2){
        if (this.data.currIndex1 < 4) {
            let currentIndex1 = this.data.currIndex1 + 1
      
            this.setData({
      
              currIndex1: currentIndex1
            })
            console.log("this" + this.data.currIndex1);
          } else
            wx.showToast({
              title: '这是最后一道选择题~',
              icon: 'none'
            })
        this.setIdiom()
    }
    if(this.data.current == 3){
        if (this.data.currIndex2 < 4) {
            let currentIndex2 = this.data.currIndex2 + 1
      
            this.setData({
      
              currIndex2: currentIndex2
            })
            console.log("this" + this.data.currIndex2);
          } else
            wx.showToast({
              title: '这是最后一道选择题~',
              icon: 'none'
            })
        this.setJian()
    }
  },
  setList() {
    //判断当前值是否存在
        if(this.data.objArr[this.data.currIndex]){
          this.setData({
            answer:this.data.objArr[this.data.currIndex].answer,
            yes: this.data.objArr[this.data.currIndex].isRIght,
            check:this.data.objArr[this.data.currIndex].check
          })
        }else{
          this.setData({
            check:0
          })
        }

  },
  setIdiom(){
        //下一题将获取的值推进数组
         if(this.data.idiomObjArr[this.data.currIndex1]){
            this.setData({
              inputAnswer:this.data.idiomObjArr[this.data.currIndex1].inputAnswer,
            })
          }else{
              this.setData({
                inputAnswer:''
              })
          }
      
  },
  setJian(){
    //下一题将获取的值推进数组
     if(this.data.jinaObjArr[this.data.currIndex2]){
        this.setData({
          textAnswer:this.data.jinaObjArr[this.data.currIndex2].textAnswer,
        })
        console.log('teashd',this.data.textAnswer);
      }else{
          this.setData({
            textAnswer:''
          })
      }
  
},
jiao(){
    console.log('111');
    if(this.data.objArr.length==5&&this.data.jinaObjArr.length==5&&this.data.idiomObjArr.length==5){

    }else{
        wx.showModal({
          title: '提示😀',
          content: '还没有全部完成哦~要不要交卷呢',
          complete: (res) => {
            if (res.cancel) {
                // this.cj()
            }
        
            if (res.confirm) {
             
             wx.showLoading({
               title: '合计分数中',
             })
             this.cj() 
             wx.hideLoading()
             wx.showModal({
               title: '成绩🤭',
               content: `分数:${this.data.cj}`,
             })
                     
            }
          }
        })
    }
},
cj(){
    //选择题 5 一道 2分  10分
    let num1 =0
    if(this.data.objArr.length>0){
      let arr = this.data.objArr.map((item)=>item.isRIght)
      //1 对 0错
        num1 =  arr.reduce((per,cur)=>(per+cur),0) * 2
    }else{
       num1 = 0
    }
   
   
    //填空题 5  一道 5分  25分 
    let num2 = 0
    if(this.data.idiomObjArr.length>0){
      let arr2 =  this.data.noChangeIdiomList.map((item)=>{
        console.log(item.question.split('，')[1]);
        item.question.split('，')[1]
    })
    arr2.forEach((item,index)=>{
        item == this.data.idiomObjArr[index].inputAnswer ? num2++ : ''
        console.log(item,this.data.idiomObjArr[index].inputAnswer,num2);
    })
       num2 = num2 * 5
    }else{
      return num2 = 0
    }
 
    //简答题 5 一道 13分  65分
    let num3 = 0
    // let num3 =0
    // let arr3 = this.data.jianList.map((item,index)=>{
    //     return item.question.split(',')[1]
    // })
    // console.log('arr3',arr3);
    // arr3.forEach((item,index)=>{
    //     item == this.data.jianList[index].answer ? num3++ :''
    //     console.log(item,this.data.jianList[index].answer,num3);
    // })
    this.data.cj = num1+num2+num3
},


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})